//
//  VRDataDetailCell.m
//  VRFirefighting
//
//  Created by Veer on 2018/8/9.
//  Copyright © 2018年 Veer. All rights reserved.
//

#import "VRDataDetailCell.h"

#import "VRDeviceInfoModel.h"

@interface VRDataDetailCell()

@property (nonatomic, assign) UIView *bottomLineView;
@property (nonatomic, weak) UIImageView *iconImgView;

@property (nonatomic, weak) UILabel *titleLbl;
@property (nonatomic, weak) UILabel *statusLbl;
@property (nonatomic, weak) UIImageView *statusImgView;
@property (nonatomic, weak) UIImageView *detailIconImgView;
@property (nonatomic, weak) UILabel *detailLbl;


@end

@implementation VRDataDetailCell


+ (instancetype)cellWith:(UITableView *)tablevie
{
    static NSString *cellID = @"JKAuthenticateCell";
    
    VRDataDetailCell *cell = [tablevie dequeueReusableCellWithIdentifier:cellID];
    
    if (nil == cell) {
        
        cell = [[VRDataDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.accessoryType = UITableViewCellAccessoryNone;
        // lieview
        [self setupSubView];
        
    }
    return self;
}


- (void)setupSubView
{
    
    UIView *selectedBgView = [[UIView alloc] init];
    selectedBgView.backgroundColor = RGB(238, 238, 238);
    self.selectedBackgroundView = selectedBgView;
    
    UIImageView *iconImgView = [[UIImageView alloc] init];
    iconImgView.contentMode = UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:iconImgView];
    self.iconImgView = iconImgView;
    
    UILabel *titleLbl = [[UILabel alloc] init];
    titleLbl.numberOfLines = 2;
    titleLbl.textColor = RGB(80, 80, 80);
    titleLbl.font = VRFont(16);
    titleLbl.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:titleLbl];
    self.titleLbl = titleLbl;
    
    UILabel *statusLbl = [[UILabel alloc] init];
    statusLbl.textColor = RGB(255, 255, 255);
    statusLbl.font = VRFont(14);
    statusLbl.textAlignment = NSTextAlignmentRight;
    statusLbl.layer.cornerRadius = 2;
    statusLbl.layer.masksToBounds = YES;
    statusLbl.backgroundColor = [UIColor redColor];
    statusLbl.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:statusLbl];
    self.statusLbl = statusLbl;
    
    UIImageView *statusImgView = [[UIImageView alloc] init];
    statusImgView.contentMode = UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:statusImgView];
    self.statusImgView = statusImgView;
    
    UIImageView *detailIconImgView = [[UIImageView alloc] init];
    detailIconImgView.contentMode = UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:detailIconImgView];
    self.detailIconImgView = detailIconImgView;
    
    UILabel *detailLbl = [[UILabel alloc] init];
    detailLbl.text = @"数据列表";
    detailLbl.textColor = RGB(140, 140, 140);
    detailLbl.font = VRFont(16);
    detailLbl.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:detailLbl];
    self.detailLbl = detailLbl;
    
    UIView *bottomLineView = [[UIView alloc] init];
    bottomLineView.backgroundColor = RGB(228, 228, 228);
    [self.contentView addSubview:bottomLineView];
    self.bottomLineView = bottomLineView;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerY.mas_equalTo(self.contentView);
        make.width.mas_equalTo(26);
        make.height.mas_equalTo(26);
    }];
    
    [self.titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconImgView.mas_right).offset(6);
        make.centerY.mas_equalTo(self.contentView);
        if (kScreenWidth < 375) {
            make.width.mas_equalTo(50);
        }
    }];
    
    [self.statusLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLbl.mas_right).offset(6);
        make.centerY.mas_equalTo(self.contentView);
        make.width.mas_equalTo(48);
        make.height.mas_equalTo(20);
    }];
    
    [self.statusImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.statusLbl.mas_right).offset(6);
        make.centerY.mas_equalTo(self.contentView);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];
    
    [self.detailIconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.detailLbl.mas_left).offset(-6);
        make.centerY.mas_equalTo(self.contentView);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];
    
    [self.detailLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.centerY.mas_equalTo(self.contentView);
    }];
    
    [self.bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(self.contentView);
        make.bottom.mas_equalTo(self.contentView);
        make.height.mas_equalTo(0.5);
    }];
}


#pragma mark - setter && getter
- (void)setModel:(VRDeviceInfoModel *)model
{
    _model = model;
    
    NSDictionary *typeDict = @{@(0) : @{@"name": @"正常", @"color": RGB(78, 173, 57), @"icon": @"i_data_success"}, @(1) : @{@"name": @"报警", @"color": RGB(238, 11, 25), @"icon": @"i_data_error"}, @(3) : @{@"name": @"故障", @"color": RGB(251, 86, 31), @"icon": @"i_data_error"}};
    
    NSDictionary *typeIconDict = @{@(0) : @"i_ynd", @(1) : @"i_yyhtnd", @(3) : @"i_dy"};
    self.iconImgView.image = [UIImage imageNamed:typeIconDict[@(model.fType)]];
    self.titleLbl.text = model.fTypeName;
    
    self.statusLbl.backgroundColor = typeDict[@(model.fState)][@"color"];
    self.statusLbl.text = typeDict[@(model.fState)][@"name"];
    self.statusImgView.image = [UIImage imageNamed:typeDict[@(model.fState)][@"icon"]];
    
    self.detailIconImgView.image = [UIImage imageNamed:typeDict[@(model.fState)][@"icon"]];
    self.detailLbl.textColor = typeDict[@(model.fState)][@"color"];
    
    if (model.fState == 1) {
        
//        self.statusLbl.text = @"异常";
//        self.statusLbl.backgroundColor = RGB(225, 11, 24);
//        self.statusImgView.image = [UIImage imageNamed:@"i_data_error"];
//        self.detailIconImgView.image = [UIImage imageNamed:@"i_data_error"];
//        self.detailLbl.textColor = RGB(225, 11, 24);
    }else{
        
//        self.statusLbl.text = @"正常";
//        self.statusLbl.backgroundColor = RGB(78, 172, 57);
//        self.statusImgView.image = [UIImage imageNamed:@"i_data_success"];
//        self.detailIconImgView.image = [UIImage imageNamed:@"i_data_list"];
//        self.detailLbl.textColor = MAINColor;
    }
    
    if (model.fState == 0) {
        
        self.detailIconImgView.image = [UIImage imageNamed:@"i_data_list"];
        self.detailLbl.textColor = MAINColor;
    }
    
}

@end
